package com.lxs.mall.mallseckill.interceptor;

import com.lxs.common.constant.AuthConstant;
import com.lxs.common.vo.MemberVO;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 秒杀活动拦截器，确认是在用户登录下的操作
 */
public class AuthInterceptor implements HandlerInterceptor {
    public static ThreadLocal threadLocal=new ThreadLocal();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //通过HttpSession获取用户的登录的信息
        HttpSession httpSession=request.getSession();
        Object attribute = httpSession.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        if(attribute!=null){
            MemberVO memberVO= (MemberVO) attribute;
            threadLocal.set(memberVO);
            return true;
        }
        //如果attribute为null说明没有登录，那么我们需要重定向到登录页面
        httpSession.setAttribute(AuthConstant.AUTH_SESSION_MSG,"请先登录！");
        response.sendRedirect("http://auth.lxsde.com/login.html");


        return false;
    }
}
